package net.melon.article.inst.ssh.manage;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sun.xml.internal.ws.api.server.Module;

import net.esj.basic.core.view.tools.ActionContext;
import net.esj.basic.dao.querybuilder.QueryBuilder;
import net.esj.basic.exception.MelonException;
import net.esj.basic.pojo.op.Operator;
import net.esj.basic.service.impl.DefaultServiceImpl;
import net.esj.basic.utils.CollectionUtils;
import net.esj.basic.utils.DateUtils;
import net.esj.basic.utils.Validators;
import net.melon.article.ArticleConstants;
import net.melon.article.manager.ArticleManage;
import net.melon.article.pojo.Article;
import net.melon.article.pojo.Channel;
import net.melon.page.pojo.ModuleLink;
import net.melon.page.pojo.ModuleLinkType;
import net.melon.uploader.Uploader;
import net.melon.uploader.pojo.FileUpload;
import net.melon.uploader.service.FileNameRule;
import net.melon.uploader.service.PathRule;
import net.melon.uploader.service.impl.DateSplitPathRule;
import net.melon.uploader.service.impl.RandomFileNameRule;

@Service
public class ArticleManageImpl extends DefaultServiceImpl implements ArticleManage,ArticleConstants {

	FileNameRule fileNameRule = new RandomFileNameRule();
	
	PathRule pathRule = new DateSplitPathRule();
	
	@Override
	public Article saveArticle(Article article, Operator operator)
			throws MelonException,Exception {
		Uploader uploader = new Uploader(pathRule, fileNameRule, ARTICLE_CONTENT_ICON_UPLOAD_KEY, new String[]{"entity.icon"}, new Object[]{article});
		List<FileUpload> list = uploader.upload((HttpServletRequest) ActionContext.getRequest());
		if(!CollectionUtils.isEmpty(list)){
			FileUpload icon = list.get(0);
			article.setIcon(icon.getUrl());
		}
		article.setGmtCreate(DateUtils.now());
		article.setCreator(operator.getId().toString());
		article.setCreatorType(operator.getMType());
		save(article);
		return article;
	}

	@Override
	public Article updateArticle(Article article) throws MelonException,
			Exception {
		Article existArticle = get(article.getId(), Article.class);
		existArticle.setTitle(article.getTitle());
		existArticle.setParam(article.getParam());
		existArticle.setParam_2(article.getParam_2());
		existArticle.setParam_3(article.getParam_3());
		existArticle.setDescribe(article.getDescribe());
		existArticle.setContent(article.getContent());
		Uploader uploader = new Uploader(pathRule, fileNameRule, ARTICLE_CONTENT_ICON_UPLOAD_KEY, new String[]{"entity.icon"}, new Object[]{article});
		List<FileUpload> list = uploader.upload((HttpServletRequest) ActionContext.getRequest());
		if(!CollectionUtils.isEmpty(list)){
			FileUpload icon = list.get(0);
			existArticle.setIcon(icon.getUrl());
		}
		update(existArticle);
		return existArticle;
	}

	@Override
	public List<Channel> getChannelList() {
		return findAll(Channel.class);
	}

	@Transactional
	@Override
	public void deleteArticle(Integer id) {
		Article article = get(id, Article.class);
		QueryBuilder<ModuleLink> qb = createQueryBuilder(ModuleLink.class);
		if(article.getChannel().getId()==1){
			qb.eq("type", new ModuleLinkType(4)).eq("value", String.valueOf(id));
		}else if(article.getChannel().getId()==2){
			qb.eq("type", new ModuleLinkType(5)).eq("value", String.valueOf(id));
		}else if(article.getChannel().getId()==3){
			qb.eq("type", new ModuleLinkType(6)).eq("value", String.valueOf(id));
		}
		List<ModuleLink> list = queryListByQueryBuilder(qb);
		if(!Validators.isEmpty(list)){
			deleteAll(list);
		}
		delete(article);
		
	}

}
